﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using AMS.Configuration;

namespace AMS.UI.Controls
{
    /// <summary>
    /// 带字典的下拉框
    /// </summary>
    public class DictComboBox : DevComponents.DotNetBar.Controls.ComboBoxEx
    {
        /// <summary>
        /// 带字典的下拉框
        /// </summary>
        public DictComboBox()
        {
            DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
        }

        private int dictIndex = -1;
        private bool showSubKey = true;
        DictComboBoxDecorector decorector;
        /// <summary>
        /// 数字字典号
        /// </summary>
        [Description("数字字典号")]
        [DefaultValue(-1)]
        public int DictIndex
        {
            get { return dictIndex; }
            set
            {
                if (dictIndex != value)
                {
                    dictIndex = value;
                    if (!ControlUtility.DesignMode)
                    {
                        BindData();
                    }
                }
            }
        }

        /// <summary>
        /// 是否显示 key - value形式，还是只显示 value的形式
        /// </summary>
        [DefaultValue(true)]
        public bool ShowSubKey
        {
            get { return showSubKey; }
            set
            {
                if (showSubKey != value)
                {
                    showSubKey = value;
                    if (!ControlUtility.DesignMode && Items.Count > 0)
                    {
                        BindData();
                    }
                }
            }
        }

        /// <summary>
        /// 获取或设置选中的数据字典
        /// </summary>
        [Browsable(false)]
        public string SelectedDictKey
        {
            get { return GetSelectedSubKey(); }
            set { Select(value); }
        }

        private void BindData()
        {
            decorector = new DictComboBoxDecorector(this, dictIndex, showSubKey, DictItemSort.SortByKeyAscending);
            decorector.DataBind();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="subKey"></param>
        public void Select(string subKey)
        {
            decorector.SelectItem(subKey);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string GetSelectedSubKey()
        {
            return decorector.GetSelectedDictKey();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string GetSelectedValue()
        {
            return decorector.GetSelectedDictValue();
        }
    }
}
